package com.seekprice.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.seekprice.core.Extractor;
import com.seekprice.core.Product;
import com.seekprice.core.impl.Buy360Extractor;
import com.seekprice.core.impl.NeweggExtractor;

@Controller
public class SearchController {

	private static Logger logger = LoggerFactory
			.getLogger(SearchController.class);

	private static Set<Extractor> extractorQueue = new HashSet<Extractor>();

	static {
		extractorQueue.add(new Buy360Extractor());
		extractorQueue.add(new NeweggExtractor());
	}

	@RequestMapping(value = "/search")
	public ModelAndView search(String k) {
		logger.debug("查询关键字为:" + k);

		List<Product> products = new ArrayList<Product>();
		for (Extractor extractor : extractorQueue) {
			products.addAll(extractor.search(k));
		}
		Map<String,Object> map=new ModelMap();
		map.put("k", k);
		map.put("products", products);
		
		return new ModelAndView("/result", map);
	}
}
